//
//  UIBarButtonItemExtension.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/6/29.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit

extension UIBarButtonItem {

    // 为了能在创建完item的时候就有button
    convenience init(imageName: String,hilightedImage:String?){
        // 创建button
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        let hilightName = hilightedImage ?? "\(imageName)_highlighted"
        btn.setImage(UIImage(named: hilightName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        
        self.init(customView: btn)
    }
    
    convenience init(imageName: String,hilightedImage:String?,target:AnyObject?,actionName:String?){
        // 创建button
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        let hilightName = hilightedImage ?? "\(imageName)_highlighted"
        btn.setImage(UIImage(named: hilightName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        // 添加监听方法
        if actionName != nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        self.init(customView: btn)
    }
}
